home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.undo.UndoableEdit;
- import java.io.Serializable;
- import java.util.Vector;
-
- public final class StringContent implements AbstractDocument.Content, Serializable {
- private static final char[] empty = new char[0];
- private char[] data;
- private int count;
- transient Vector marks;
-
- public StringContent() {
- this(10);
- }
-
- public StringContent(int initialLength) {
- if (initialLength < 1) {
- initialLength = 1;
- }
-
- this.data = new char[initialLength];
- this.data[0] = '\n';
- this.count = 1;
- }
-
- public Position createPosition(int offset) throws BadLocationException {
- if (this.marks == null) {
- this.marks = new Vector();
- }
-
- return new StickyPosition(this, offset);
- }
-
- public void getChars(int where, int len, Segment chars) throws BadLocationException {
- if (where + len > this.count) {
- throw new BadLocationException("Invalid location", this.count);
- } else {
- chars.array = this.data;
- chars.offset = where;
- chars.count = len;
- }
- }
-
- protected Vector getPositionsInRange(Vector v, int offset, int length) {
- int n = this.marks.size();
- int end = offset + length;
- Vector placeIn = v == null ? new Vector() : v;
-
- for(int i = 0; i < n; ++i) {
- PosRec mark = (PosRec)this.marks.elementAt(i);
- if (mark.unused) {
- this.marks.removeElementAt(i);
- --i;
- --n;
- } else if (mark.offset >= offset && mark.offset <= end) {
- placeIn.addElement(new UndoPosRef(this, mark));
- }
- }
-
- return placeIn;
- }
-
- public String getString(int where, int len) throws BadLocationException {
- if (where + len > this.count) {
- throw new BadLocationException("Invalid range", this.count);
- } else {
- return new String(this.data, where, len);
- }
- }
-
- public UndoableEdit insertString(int where, String str) throws BadLocationException {
- if (where >= this.count) {
- throw new BadLocationException("Invalid location", this.count);
- } else {
- char[] chars = str.toCharArray();
- this.replace(where, 0, chars, 0, chars.length);
- if (this.marks != null) {
- this.updateMarksForInsert(where, str.length());
- }
-
- return new InsertUndo(this, where, str.length());
- }
- }
-
- public int length() {
- return this.count;
- }
-
- public UndoableEdit remove(int where, int nitems) throws BadLocationException {
- if (where + nitems >= this.count) {
- throw new BadLocationException("Invalid range", this.count);
- } else {
- String removedString = this.getString(where, nitems);
- UndoableEdit edit = new RemoveUndo(this, where, removedString);
- this.replace(where, nitems, empty, 0, 0);
- if (this.marks != null) {
- this.updateMarksForRemove(where, nitems);
- }
-
- return edit;
- }
- }
-
- void replace(int offset, int length, char[] replArray, int replOffset, int replLength) {
- int delta = replLength - length;
- int src = offset + length;
- int nmove = this.count - src;
- int dest = src + delta;
- if (this.count + delta >= this.data.length) {
- int newLength = Math.max(2 * this.data.length, this.count + delta);
- char[] newData = new char[newLength];
- System.arraycopy(this.data, 0, newData, 0, offset);
- System.arraycopy(replArray, replOffset, newData, offset, replLength);
- System.arraycopy(this.data, src, newData, dest, nmove);
- this.data = newData;
- } else {
- System.arraycopy(this.data, src, this.data, dest, nmove);
- System.arraycopy(replArray, replOffset, this.data, offset, replLength);
- }
-
- this.count += delta;
- }
-
- void resize(int ncount) {
- char[] ndata = new char[ncount];
- System.arraycopy(this.data, 0, ndata, 0, Math.min(ncount, this.count));
- this.data = ndata;
- }
-
- synchronized void updateMarksForInsert(int offset, int length) {
- if (offset == 0) {
- offset = 1;
- }
-
- int n = this.marks.size();
-
- for(int i = 0; i < n; ++i) {
- PosRec mark = (PosRec)this.marks.elementAt(i);
- if (mark.unused) {
- this.marks.removeElementAt(i);
- --i;
- --n;
- } else if (mark.offset >= offset) {
- mark.offset += length;
- }
- }
-
- }
-
- synchronized void updateMarksForRemove(int offset, int length) {
- int n = this.marks.size();
-
- for(int i = 0; i < n; ++i) {
- PosRec mark = (PosRec)this.marks.elementAt(i);
- if (mark.unused) {
- this.marks.removeElementAt(i);
- --i;
- --n;
- } else if (mark.offset >= offset + length) {
- mark.offset -= length;
- } else if (mark.offset >= offset) {
- mark.offset = offset;
- }
- }
-
- }
-
- protected void updateUndoPositions(Vector positions) {
- for(int counter = positions.size() - 1; counter >= 0; --counter) {
- UndoPosRef ref = (UndoPosRef)positions.elementAt(counter);
- if (ref.rec.unused) {
- positions.removeElementAt(counter);
- } else {
- ref.resetLocation();
- }
- }
-
- }
- }
-